package org.litu.app.controller.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.litu.app.entity.system.SysSystemVersion;
import org.litu.app.service.ISysSystemVersionService;
import org.litu.base.controller.BaseFormController;
import org.litu.base.log.LtLog;
import org.litu.core.base.ApiRes;
import org.litu.core.exception.LtServerException;
import org.litu.core.login.TokenCheck;
import org.litu.util.common.SysMsgUtil;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统版本管理Controller
 *
 * @author yueye
 */
@RestController
@LtLog(module = "系统版本管理表")
@RequestMapping("/system/sysSystemVersion")
@Api(value = "系统版本管理表", tags = {"系统版本管理表"}, protocols = "http,https")
public class SysSystemVersionController extends BaseFormController<SysSystemVersion, ISysSystemVersionService> {

    /**
     * 检测是否是最新版本，若不是返回最新下载地址
     *
     * @param version 版本号
     * @return 最新版本
     */
    @GetMapping("/checkUpdate")
    @ResponseBody
    @ApiOperation(value = "版本更新", notes = "如果版本为最新版本，则返回ok，如果版本为历史版本，则返回最新版本信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemCode", value = "系统编码", dataType = "String"),
            @ApiImplicitParam(name = "platType", value = "平台类型：android，web，ios，tv", dataType = "String"),
            @ApiImplicitParam(name = "version", value = "版本号", dataType = "String")
    })
    public ApiRes<SysSystemVersion> checkUpdate(@RequestParam(name = "systemCode") String systemCode,
                                                @RequestParam(name = "platType", defaultValue = "android") String platType,
                                                @RequestParam(defaultValue = "") String version) {
        try {
            SysSystemVersion systemVersion = service.getNewSystemVersion(systemCode, platType);
            String newestVersion = systemVersion.getVersion();
            int res = SysMsgUtil.compareVersion(version, newestVersion);
            // 客户版本与服务端最新版本相同时，则不需要进行更新下载。
            // 当版本一致时，则需要重新下载。 实际不合理，版本正常是都在不断升高的，这里主要是避免版本回退的情况。
            if (res == 0) {
                return ApiRes.ok();
            } else {
                return ApiRes.ok(systemVersion);
            }
        } catch (LtServerException le) {
            logger.error("检查版本更新失败", le);
            return ApiRes.error(le.getMessage());
        } catch (Exception e) {
            logger.error("检查本更新失败", e);
            return ApiRes.error(e.getMessage());
        }
    }

    @GetMapping(value = "/getAppDownloadUrl")
    @ApiOperation(value = "App最新版本的下载Url获取", notes = "无参数")
    @ApiImplicitParam(name = "systemCode", value = "系统编码", dataType = "String")
    @TokenCheck(check = false)
    public ApiRes<Map<String, String>> getAppDownloadUrl(@RequestParam(name = "systemCode") String systemCode) {
        try {
            Map<String, String> downloadUrls = new HashMap<>(2);

            SysSystemVersion androidVersion = service.getNewSystemVersion(systemCode, "android");
            if (null != androidVersion) {
                downloadUrls.put("android", androidVersion.getDownloadPath());
            }

            SysSystemVersion iosVersion = service.getNewSystemVersion(systemCode, "ios");
            if (null != iosVersion) {
                downloadUrls.put("ios", androidVersion.getDownloadPath());
            }

            return ApiRes.ok(downloadUrls);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiRes.error(e.getMessage());
        }
    }

}
